package com.whfc.iot.mach.client;

import com.whfc.common.enums.DeviceType;
import com.whfc.common.enums.WorkState;
import com.whfc.common.util.DateUtil;
import com.whfc.common.util.JSONUtil;
import com.whfc.iot.mach.constant.MsgConst;
import com.whfc.iot.mach.entity.*;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.concurrent.TimeUnit;

public class EquipClientHandler extends ChannelInboundHandlerAdapter {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        logger.info("==============channel--register==============");
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        logger.info("==============channel--unregistered==============");
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        logger.info("==============channel--inactive==============");
    }

    // 连接成功后，向server发送消息
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        logger.info("==============channel--active==============");

        for (int cnt = 0; cnt < 1; cnt++) {

            //发送0x03
            MachMsg0x03 msg0x03 = this.createMachMsg0x03();
            ctx.channel().writeAndFlush(msg0x03);
            logger.info("发送0x03:{}", JSONUtil.toPrettyString(msg0x03));

            // 发送0x04
            MachMsg0x04 machMsg0x04 = createMachMsg0x04();
            ctx.channel().writeAndFlush(machMsg0x04);
            logger.info("发送0x04:{}", JSONUtil.toPrettyString(machMsg0x04));
        }

    }

    // 接收server端的消息，并打印出来
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object obj) throws Exception {
        logger.info("==============channel--read==============");
        MachMsg machMsg = (MachMsg) obj;
        switch (machMsg.getCmd()) {
            case MsgConst.DOWNSTREAM_CMD_0x82:
                MachMsg0x82 msg0x82 = (MachMsg0x82) machMsg;
                logger.info("接收0x82:{}", JSONUtil.toPrettyString(msg0x82));
                TimeUnit.SECONDS.sleep(15);
                MachMsg0x02 machMsg0x02 = createMachMsg0x02(msg0x82);
                ctx.writeAndFlush(machMsg0x02);
                logger.info("发送0x02:{}", JSONUtil.toPrettyString(machMsg0x02));
                break;
            case MsgConst.DOWNSTREAM_CMD_0x83:
                logger.info("接收0x83:{}", JSONUtil.toPrettyString(machMsg));
                TimeUnit.SECONDS.sleep(15);
                MachMsg0x04 machMsg0x04_1 = createMachMsg0x04();
                ctx.writeAndFlush(machMsg0x04_1);
                logger.info("发送0x04:{}", JSONUtil.toPrettyString(machMsg0x04_1));
                break;
            case MsgConst.DOWNSTREAM_CMD_0x84:
                logger.info("接收0x84:{}", JSONUtil.toPrettyString(machMsg));
                TimeUnit.SECONDS.sleep(15);
                MachMsg0x04 machMsg0x04_2 = createMachMsg0x04();
                ctx.writeAndFlush(machMsg0x04_2);
                logger.info("发送0x04:{}", JSONUtil.toPrettyString(machMsg0x04_2));
                break;
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }

    private MachMsg0x02 createMachMsg0x02(MachMsg0x82 msg0x82) {
        MachMsg0x02 msg = new MachMsg0x02();

        msg.setStartFlag(MsgConst.START_FLAG);
        msg.setDeviceType(DeviceType.equip.getValue());
        msg.setDeviceId(0x01);
        //msg.setLength(data0x02.getDataLength() + 11)
        msg.setSeq(2000);
        msg.setCmd(MsgConst.UPSTREAM_CMD_0x02);

        msg.setMasterOrSlave(1);
        msg.setVer("1.0.12");
        msg.setSubPackageTotal(msg0x82.getSubPackageTotal());
        msg.setSubPackageSeq(msg0x82.getSubPackageSeq());
        msg.setResult(1);
        return msg;
    }

    private MachMsg0x03 createMachMsg0x03() {
        MachMsg0x03 msg = new MachMsg0x03();

        msg.setStartFlag(MsgConst.START_FLAG);
        msg.setDeviceType(DeviceType.equip.getValue());
        msg.setDeviceId(2000);
        //msg.setLength(data0x02.getDataLength() + 11)
        msg.setSeq(0x01);
        msg.setCmd(MsgConst.UPSTREAM_CMD_0x03);
        return msg;
    }

    private MachMsg0x04 createMachMsg0x04() {
        Date now = new Date();
        MachWorkState machWorkState = new MachWorkState(DateUtil.addMinutes(now, -0), WorkState.RUN.getValue());

        MachMsg0x04 msg = new MachMsg0x04();
        msg.setStartFlag(MsgConst.START_FLAG);
        msg.setDeviceType(DeviceType.equip.getValue());
        msg.setDeviceId(2000);
        msg.setSeq(0x01);
        msg.setCmd(MsgConst.UPSTREAM_CMD_0x04);

        msg.setWorkState(machWorkState);
        msg.setBatteryPower(81);
        msg.setLngFlag("E");
        msg.setLatFlag("N");
//        msg.setLngWgs84(114.421356D);
//        msg.setLatWgs84(30.472532D);
        msg.setLngWgs84(114.383877D);
        msg.setLatWgs84(30.483314D);
        msg.setMasterVer("1.0.0");
        return msg;
    }

}
